package com.bems.service;

import java.util.List;
import java.util.Map;
import java.util.Set;

import com.bems.jdbc.common.QueryHelper;
import com.bems.pojo.Bill;
import com.bems.util.PageUtil;

public class BillService 
{
	public Float cost(String starttime,String endtime,Map<String, Object> map)
	{
		String filter = " ";
		 Set<String> sets = map.keySet();
		 int i = 0;
		  for (String string : sets) 
			  {
				  i++;
				  filter=filter+string+" = ?";
				 if (sets.size() > i) 
				 	{
					 filter=filter+" and ";
				 	}
			  }
		  filter = filter+ " and UNIX_TIMESTAMP(createtime) between  UNIX_TIMESTAMP('"+starttime+"') and UNIX_TIMESTAMP('"+endtime+"') ";
		  String sql = "SELECT SUM(cost) FROM  bems_bill  WHERE " + filter  + " ";
		return null;
		  
		//return (Float) QueryHelper.stat(sql, map.values().toArray());
		
	}
	
	public PageUtil list(String starttime,String endtime,int page,int size,Map<String, Object> map)
	{
		PageUtil pageUtil = new PageUtil();
		
		String filter = " ";
		 Set<String> sets = map.keySet();
		 int i = 0;
		  for (String string : sets) 
			  {
				  i++;
				  filter=filter+string+" = ?";
				 if (sets.size() > i) 
				 	{
					 filter=filter+" and ";
				 	}else {
						if(sets.size()>0)
							filter=filter+" and ";
					}
			  }
		  
		  filter = filter+ "  UNIX_TIMESTAMP(createtime) between  UNIX_TIMESTAMP('"+starttime+"') and UNIX_TIMESTAMP('"+endtime+"') ";
		
		  String sql = "SELECT * FROM  bems_bill  WHERE " + filter  + " ORDER BY id DESC  ";
		//return QueryHelper.query_cache("User", "group_id"+group_id, User.class, sql, group_id,(page-1)*size,size);
		// Object params= ArrayUtils.add(map.values().toArray(), element);
		List<Bill> list = QueryHelper.query_slice(Bill.class, sql, page, size, map.values().toArray());
		pageUtil.setList(list);
		pageUtil.setPage(page);
		pageUtil.setSize(size);
		
		filter = " ";
		  i = 0;
		  for (String string : sets) 
			  {
				  i++;
				  filter=filter+string+" = "+map.get(string);
				 if (sets.size() > i) 
				 	{
					 filter=filter+" and ";
				 	}else {
						if(sets.size()>0)
							filter=filter+" and ";
					}
			  }
		  filter = filter+ "  UNIX_TIMESTAMP(createtime) between  UNIX_TIMESTAMP('"+starttime+"') and UNIX_TIMESTAMP('"+endtime+"') ";
		int totalpage=new Bill().totalCount(filter);
		pageUtil.setTotalpage(totalpage);
		return pageUtil;
		
	}
	
}
